﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;


namespace CMS_GYM
{
    public class Utils
    {
        public static DataTable CreateTable(string[] columnList, Type[] valuesOfColumns)
        {
            DataTable dt = new DataTable();
            for (int i = 0; i < columnList.Length; i++)
            {
                dt.Columns.Add(columnList[i], valuesOfColumns[i]);
            }
            return dt;
        }

        public static bool IsAlpha(string strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z]");
            return !objAlphaPattern.IsMatch(strToCheck);
        }

        public static bool IsNumeric(string text)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(text);
        }


        public static int getNhomCuoc(string NHOM_CUOC)
        {
            int index = 1;
            switch (NHOM_CUOC)
            {
                case "ADVANCE":
                    index = 1;
                    break;
                case "EASY":
                    index = 3;
                    break;
                case "FAMILY":
                    index = 5;
                    break;
                case "HIGH":
                    index = 7;
                    break;
                case "SUPER":
                    index = 9;
                    break;
            }
            return index;
        }

        public static void CopyDataViewToDataTable(ref DataTable dt, DataView dtSource, int ColumnCount)
        {
            //dt.Clear();
            for (int i = 0; i < dtSource.Count; i++)
            {
                DataRow drow = dt.NewRow();
                for (int j = 0; j < dtSource.Table.Columns.Count - ColumnCount; j++)
                    drow[j] = dtSource[i][j];
                dt.Rows.Add(drow);
            }
        }

        public static int LayQuyCuaThang(string TEN_THANG, ref string NAM)
        {
            int quy = -1;
            try
            {
                int thang = Convert.ToInt32(TEN_THANG.Split('/')[0]);
                NAM = TEN_THANG.Split('/')[1];
                switch (thang)
                {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        quy = 1;
                        break;
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                        quy = 2;
                        break;
                    case 9:
                    case 10:
                    case 11:
                    case 12:
                        quy = 3;
                        break;
                }
            }
            catch (Exception) { }
            return quy;
        }

        //public static string[] LayThangCuaQuy(string quy, string nam)
        //{
        //    string[] arrThang = new string[3];
        //    switch (quy)
        //    {
        //        case "1":
        //            arrThang[0] = LayThangID("01/" + nam);
        //            arrThang[1] = LayThangID("02/" + nam);
        //            arrThang[2] = LayThangID("03/" + nam);
        //            break;
        //        case "2":
        //            arrThang[0] = LayThangID("04/" + nam);
        //            arrThang[1] = LayThangID("05/" + nam);
        //            arrThang[2] = LayThangID("06/" + nam);
        //            break;
        //        case "3":
        //            arrThang[0] = LayThangID("07/" + nam);
        //            arrThang[1] = LayThangID("08/" + nam);
        //            arrThang[2] = LayThangID("09/" + nam);
        //            break;
        //        case "4":
        //            arrThang[0] = LayThangID("10/" + nam);
        //            arrThang[1] = LayThangID("11/" + nam);
        //            arrThang[2] = LayThangID("12/" + nam);
        //            break;
        //    }
        //    return arrThang;
        //}

        //public static string[] LayTenThangCuaQuy(string quy, string nam)
        //{
        //    string[] arrThang = new string[3];
        //    switch (quy)
        //    {
        //        case "1":
        //            arrThang[0] = "01/" + nam;
        //            arrThang[1] = "02/" + nam;
        //            arrThang[2] = "03/" + nam;
        //            break;
        //        case "2":
        //            arrThang[0] = "04/" + nam;
        //            arrThang[1] = "05/" + nam;
        //            arrThang[2] = "06/" + nam;
        //            break;
        //        case "3":
        //            arrThang[0] = "07/" + nam;
        //            arrThang[1] = "08/" + nam;
        //            arrThang[2] = "09/" + nam;
        //            break;
        //        case "4":
        //            arrThang[0] = "10/" + nam;
        //            arrThang[1] = "11/" + nam;
        //            arrThang[2] = "12/" + nam;
        //            break;
        //    }
        //    return arrThang;
        //}

        public static void setColor(System.Windows.Forms.RadioButton radio)
        {
            if (radio.Checked)
                radio.ForeColor = System.Drawing.Color.DarkOrange;
            else radio.ForeColor = System.Drawing.Color.FromArgb(19, 74, 138);
        }

        public static void setColor(System.Windows.Forms.CheckBox check)
        {
            if (check.Checked)
                check.ForeColor = System.Drawing.Color.DarkOrange;
            else check.ForeColor = System.Drawing.Color.FromArgb(19, 74, 138);
        }

        /// <summary>
        /// Desc: Cắt chỗi tính từ trái qua phải n ký tự
        /// </summary>
        /// <param name="param"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Left(string param, int length)
        {
            try
            {
                string result = param.Substring(0, length);
                return result;
            }
            catch { }

            return string.Empty;
        }

        /// <summary>
        /// Desc: Cắt chỗi tính từ phải qua trái n ký tự
        /// </summary>
        /// <param name="param"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Right(string param, int length)
        {
            try
            {
                string result = param.Substring(param.Length - length, length);
                return result;
            }
            catch { }

            return string.Empty;
        }

        /// <summary>

        /// Desc: Tạo ra chuỗi ký tự xuống dòng (\r\n)
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string TextPadding(int length)
        {
            try
            {
                string result = string.Empty;
                for (int i = 0; i < length; i++) result += "\r\n";
                return result;
            }
            catch (System.Exception ex) { }

            return string.Empty;
        }

        /// <summary>

        /// Desc: Thêm n ký tự vào Text
        /// - Cắt chuỗi với chiều dài n Từ trái qua phải hoặc ngược lại
        /// </summary>
        /// <param name="strInPut">Chuỗi cần thêm</param>
        /// <param name="charToAdd">Ký tự cần thêm</param>
        /// <param name="numStrToReturn">Chiều dài chuỗi trả về</param>
        /// <param name="alignLeft">True: căn trái, False: căn phải</param>
        /// <returns></returns>
        public static string TextAddSpace(string strInPut, char charToAdd, int numCharToReturn, bool alignLeft)
        {
            try
            {
                string strSpace = string.Empty;
                for (int i = 0; i < numCharToReturn; i++) strSpace += charToAdd.ToString();//Tạo chuỗi chứa ký tự cần thêm
                strInPut = (alignLeft) ? strInPut + strSpace : strSpace + strInPut;// Cộng chỗi cần thêm vào trước hoặc sau chỗi text chuyền vào
                return (alignLeft) ? Utils.Left(strInPut, numCharToReturn) : Utils.Right(strInPut, numCharToReturn);// Lấy chuỗi căn trải hoặc căn phải
            }
            catch (System.Exception ex) { }

            return string.Empty;
        }

        /// <summary>

        /// Desc: Kiem tra chuoi co phai la so hay khong
        /// </summary>
        /// <param name="inputvalue"></param>
        /// <returns></returns>
        public static bool IsItNumber(string inputvalue)
        {
            Regex isnumber = new Regex("[^0-9]");
            return !isnumber.IsMatch(inputvalue);
        }

        /// <summary>

        /// Desc: Kiem tra ky tu nhap vao co phai la so hoac phim back khong
        /// Ham nay dung de validate chi cho nhap so
        /// </summary>
        /// <param name="myChar"></param>
        /// <returns></returns>
        public static bool OnKyPress_IsNumber(Char myChar)
        {
            return (!Char.IsDigit(myChar) && myChar != 8);
        }

        /// <summary>

        /// Desc: Kiểm tra chuỗi chỉ có chữ và số
        /// </summary>
        /// <param name="strToCheck"></param>
        /// <returns></returns>
        public static bool IsAlphaNumeric(String strToCheck)
        {
            bool result = false;
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9_@.]");
            result = !objAlphaNumericPattern.IsMatch(strToCheck);
            return result;
        }

        /// <summary>

        /// Desc:Chuyển đổi tiếng việt có dấu thành không dấu
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static string LoaiBoDauTiengViet(string strSource)
        {
            try
            {
                Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
                string strFormD = strSource.Normalize(System.Text.NormalizationForm.FormD);
                return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
            }
            catch (Exception ex)
            { }

            return string.Empty;
        }

        /// <summary>

        /// Desc:Kiểm tra số điện thoại di động
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        public static bool CheckMobilePhone(string phoneNumber)
        {
            try
            {
                Regex regex = new Regex(@"^09\d{8}$|^01\d{9}$");
                return regex.IsMatch(phoneNumber);
            }
            catch (Exception ex)
            { }
            return false;
        }
        /// <summary>

        /// Desc: Hàm kiểm tra số điện thoại SCTVFone
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns>True:Hop le,False:Khong hop le</returns>
        public static bool CheckSCTVFone(string sctvFone)
        {
            try
            {
                Regex regex = new Regex(@"^09\d{8}|^849\d{8}");
                return regex.IsMatch(sctvFone);
            }
            catch (Exception ex)
            { }
            return false;
        }

        /// <summary>

        /// Desc: Kiem tra dia chi mac
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns>True:Hop le,False:Khong hop le</returns>
        public static bool IsValidateMacAddress(string mac)
        {
            bool result = false;
            try
            {
                Regex regex = new Regex(@"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$");
                result = regex.IsMatch(mac);
            }
            catch (Exception ex)
            {
                result = false;
            }
            return result;
        }

    }

    public class DataItem
    {
        private string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        private string _Value;
        public string Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        public DataItem(string name, string value)
        {
            this.Name = name;
            this.Value = value;
        }
        public DataItem()
        {
            this.Name = string.Empty;
            this.Value = string.Empty;
        }
    }

}

